<?php

class Talktangle_PromotionController extends Core_Controller_Action_Standard
{
 
  public function uploadAction(){
    $result = array();
    $viewer = Engine_Api::_()->user()->getViewer();
	$calback = $this->_getParam('callback','resultUploadIframe');
    if(!$viewer || empty($viewer->user_id)){
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
        $data =  Zend_Json::encode($result);
        $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
        exit($js);
    }
    
     //CHECK FORM VALIDAION
    if (!$this->getRequest()->isPost()) {
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
        $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
        exit($js); 
      
    }
   

    //CHECK UPLOAD
    if (!isset($_FILES['promotion_image']) || !is_uploaded_file($_FILES['promotion_image']['tmp_name'])) {
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid Upload');
                $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
      exit($js);   
    }
    $params = array(
        'parent_id' => 0,
        'parent_type' =>'talktangle_promotion'
    );
    $photoObj = Engine_Api::_()->getDbtable('promotions', 'talktangle')->uploadPhoto($params, $_FILES['promotion_image']);
    $result['error'] = false;
    $result['id'] = $photoObj->file_id;
    $file = Engine_Api::_()->getApi('storage', 'storage')->get($photoObj->file_id, 'thumb.normal');
    $result['path'] = $file->map();
    $result['imageId'] = $photoObj->file_id;
      
            $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
 exit($js); 
  }
 
}
